package com.eco.fanliapp.service.impl;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.eco.fanliapp.common.constant.ApiEnum;
import com.eco.fanliapp.service.IAlipayService;
import com.eco.fanliapp.utils.UUIDUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class AlipayServiceImpl implements IAlipayService {


    @Override
    public AlipayFundTransToaccountTransferResponse payUserToAlipay(String alipayAccount, String alipayNickName, BigDecimal money,Long userId) {
        AlipayClient alipayClient = new DefaultAlipayClient(ApiEnum.GATEWAYURL, ApiEnum.APP_ID, ApiEnum.MERCHANT_PRIVATE_KEY, "json", ApiEnum.CHARSET, ApiEnum.ALIPAY_PUBLIC_KEY, ApiEnum.SIGN_TYPE); //获得初始化的AlipayClient
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        String orderId = UUIDUtils.getOrderIdByUUId()+"_"+userId;
        request.setBizContent("{" +
                "\"out_biz_no\":\""+orderId+"\"," +
                "\"payee_type\":\"ALIPAY_LOGONID\"," +
                "\"payee_account\":\""+alipayAccount+"\"," +
                "\"amount\":\""+money+"\"," +
    //            "\"payer_real_name\":\"郑州星分科技\"," +
                "\"payer_show_name\":\"省鸭App提现\"," +
                "\"payee_real_name\":\""+alipayNickName+"\"," +
                "\"remark\":\"省鸭App提现\"," +
                "  }");
        AlipayFundTransToaccountTransferResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return response;
    }

}
